/*
 * Exploder
 * 
 * Tero Pyylampi (218437), Jussi Tuurinkoski (211594)
 *
 * Simple dialog to be used
 */

#include "Dialog.h"
#include "../Resources.h"
#include "../Common.h"

using namespace cocos2d;

// default constructor
Dialog::Dialog( std::string dialogTitle, float titleSize ) {
	// layer settings
	setContentSize( CCSize( WIN_WIDTH, WIN_HEIGHT ) );
	setAnchorPoint( ccp( 0.0f, 0.0f ) );
	setPosition( 0.0f, 0.0f );

	// background background to prevent clicking other items on the background
	CCMenuItemImage *massBackground = CCMenuItemImage::itemFromNormalImage( getResource( Textures::GAME_MISC_INVISIBLE ), getResource( Textures::GAME_MISC_INVISIBLE ) );
	massBackground->setScaleX( RATEX * 10000.0f );
	massBackground->setPosition( ccp( WIN_WIDTH * 0.5f, WIN_HEIGHT * 0.5f ) );

	// background
	CCMenuItemImage* bBackground = CCMenuItemImage::itemFromNormalImage( getResource( Textures::MENU_BACKGROUND_NOTEBOARD ), getResource( Textures::MENU_BACKGROUND_NOTEBOARD ) );
	bBackground->setScale( RATEX * 1.55f );
	bBackground->setPosition( ccp( WIN_WIDTH * 0.5f, WIN_HEIGHT * 0.5f ) );

	// title
	CCLabelBMFont* llTitle = CCLabelBMFont::labelWithString( dialogTitle.c_str(), getResource( Fonts::FONT_NORMAL ) );
	CCMenuItemLabel *title = CCMenuItemLabel::itemWithLabel( llTitle );
	title->setPosition( ccp( WIN_WIDTH * 0.5f, WIN_HEIGHT * 0.65f ) );
	title->setScale( RATEX * 0.83f );

	// create menu
	mMenu = CCMenu::menuWithItems( massBackground, bBackground, title, NULL );
	mMenu->setPosition( CCPointZero );
	addChild( mMenu, Z_LAYER_MENU + 1 );
}

// default update function
void Dialog::update( cocos2d::ccTime dt ) {
	if( mFadeTimeCounter < mTotalFadeTime ) {
		mFadeTimeCounter += dt;
		if( mFadeTimeCounter >= mTotalFadeTime )
			mFadeTimeCounter = mTotalFadeTime;
		float opacity = 255.0f * mFadeTimeCounter / mTotalFadeTime;
		mMenu->setOpacity( opacity );
		for( unsigned int i = 0; i < mSprites.size(); ++i )
			mSprites.at( i )->setOpacity( opacity );
	}
}

// add a text component to the dialog
CCLabelBMFont* Dialog::addText( std::string text, float x, float y ) {
	CCLabelBMFont *label = CCLabelBMFont::labelWithString( text.c_str( ), getResource( Fonts::FONT_NORMAL ) );
	label->setScaleX( RATEX * 0.6f);
	label->setScaleY( RATEY * 0.6f );
	label->setPosition( ccp( x, y ) );
	mMenu->addChild( label, Z_LAYER_MENU + 1 );
	return label;
}

// add a button to the dialog
void Dialog::addTextButton( CCObject* target, SEL_MenuHandler selector, std::string text, float x, float y, CCSize scale ) {
	// text above button
	cocos2d::CCLabelBMFont *uaLabel = CCLabelBMFont::labelWithString( text.c_str( ), getResource( Fonts::FONT_NORMAL ) );
	cocos2d::CCMenuItemLabel *label = cocos2d::CCMenuItemLabel::itemWithLabel( uaLabel, target, selector );
	uaLabel->setAlignment( CCTextAlignmentCenter );
	label->setScale( RATEX * scale.height );
	label->setPosition( ccp( x, y ) );

	// add items
	mMenu->addChild( label, Z_LAYER_MENU + 1 );
}

// add a sprite to the dialog
void Dialog::addSprite( char *sprite, float x, float y, cocos2d::CCSize scale ) {
	cocos2d::CCSprite *kuva = cocos2d::CCSprite::spriteWithFile( getResource( sprite ) );
	kuva->setScale( RATEX * scale.width );
	kuva->setPosition( ccp( x, y ) );
	addChild( kuva, Z_LAYER_MENU + 1 );
	mSprites.push_back( kuva );
}

// default destructor
Dialog::~Dialog() {}

// show dialog with a wanted time
void Dialog::showDialogFadeIn( float fadeTime ) {
	mTotalFadeTime = fadeTime;
	mFadeTimeCounter = 0.0f;
}

// hide dialog with a wanted time
void Dialog::hideDialogFadeOut( float fadeTime ) {
	mTotalFadeTime = fadeTime;
	mFadeTimeCounter = fadeTime;
}